#!/usr/bin/env python
#_*_coding:utf-8_*_
#QQ 7796755
#Blog:www.qinglin.net
#author = "Guanqinglin"
from __future__ import absolute_import, unicode_literals
import os
import datetime
from celery import Celery, platforms
from celery.schedules import crontab
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_django_statudy.settings')  #配置Django环境

app = Celery('celery_django_statudy') #配置django项目名称

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')  #使用CELERY_ 作为前缀，在settings中写配置

# Load task modules from all registered Django app configs.
app.autodiscover_tasks() #发现任务文件每个app下的tasks.py

# 允许root 用户运行celery
platforms.C_FORCE_ROOT = True


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))


# CELERYBEAT_SCHEDULE = {
#     'add-every-30-seconds': {
#          'task': 'statudy_celery.tasks.add',
#          'schedule': datetime.timedelta(seconds=30),
#          'args': (16, 16)
#     },
# }

CELERY_TIMEZONE = 'Asia/Shanghai'
app.conf.update(
    CELERYBEAT_SCHEDULE = {
        'sum-task': {
            'task': 'statudy_celery.tasks.add',
            'schedule':  datetime.timedelta(seconds=20),
            'args': (5, 6)
        },
        # 'send-report': {
        #     'task': 'deploy.tasks.report',
        #     #'schedule': crontab(hour=4, minute=30, day_of_week=1),
        #     'schedule': crontab(),
        # }
    }
)